package ahblue.cn.tanzi.ui.home.search;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.widget.EditText;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

import java.util.ArrayList;
import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.jmessage.im.adapter.TextWatcherAdapter;
import ahblue.cn.tanzi.model.home.ArticleCategory;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.ui.home.article.ArticleListFragment;
import ahblue.cn.tanzi.util.DensityUtils;
import ahblue.cn.tanzi.view.ScaleTransitionPagerTitleView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 首页搜索关键字后返回的搜索结果页面。
 */
public class SearchResultActivity extends BaseActivity {

    @BindView(R.id.edit_search)
    EditText mEditTextSearch;

    @BindView(R.id.tl_article_page_indicator)
    MagicIndicator mTabLayoutPage;
    @BindView(R.id.vp_article_viewpager)
    ViewPager mViewPagerArticle;

    public static final String ARG_SEARCH_WORD = "ARG_SEARCH_WORD";

    private final List<ArticleCategory> mArticleCategoryList = new ArrayList<>();

    private String mSearchWord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);

        ButterKnife.bind(this);

        Intent intent = getIntent();
        mSearchWord = intent.getStringExtra(ARG_SEARCH_WORD);
        if (mSearchWord.length() == 0) {
            finish();
            return;
        }

        initData();
        initView();
    }

    private void initData() {
        ArticleCategory ac = new ArticleCategory();
        ac.setId(1);
        ac.setCategoryTitle("文章");
        mArticleCategoryList.add(ac);

        ac = new ArticleCategory();
        ac.setId(2);
        ac.setCategoryTitle("视频");
        mArticleCategoryList.add(ac);

        ac = new ArticleCategory();
        ac.setId(3);
        ac.setCategoryTitle("专题");
        mArticleCategoryList.add(ac);

        ac = new ArticleCategory();
        ac.setId(4);
        ac.setCategoryTitle("圈子");
        mArticleCategoryList.add(ac);

        ac = new ArticleCategory();
        ac.setId(5);
        ac.setCategoryTitle("好友");
        mArticleCategoryList.add(ac);

        ac = new ArticleCategory();
        ac.setId(6);
        ac.setCategoryTitle("动态");
        mArticleCategoryList.add(ac);
    }

    private void initView() {
        mEditTextSearch.setText(mSearchWord);
        mEditTextSearch.setSelection(mEditTextSearch.getText().length());
        mEditTextSearch.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                mSearchWord = s.toString();
                List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
                for (int i = 0; i < fragmentList.size(); i++) {
                    ((ArticleListFragment)fragmentList.get(i)).setSearchWord(mSearchWord);
                }
            }
        });
        ArticleCategoryAdapter adapter = new ArticleCategoryAdapter(getSupportFragmentManager(), mArticleCategoryList);
        mViewPagerArticle.setAdapter(adapter);

        CommonNavigator cn = new CommonNavigator(this);
        cn.setAdapter(mNavigatorAdapter);

        mTabLayoutPage.setNavigator(cn);
        ViewPagerHelper.bind(mTabLayoutPage, mViewPagerArticle);
    }



    @OnClick(R.id.tv_search_cancle)
    void onClickSearchCancle() {
        finish();
    }

    CommonNavigatorAdapter mNavigatorAdapter = new CommonNavigatorAdapter() {

        @Override
        public int getCount() {
            return mArticleCategoryList.size();
        }

        @Override
        public IPagerTitleView getTitleView(Context context, int index) {
            ScaleTransitionPagerTitleView colorTransitionPagerTitleView = new ScaleTransitionPagerTitleView(context);
            colorTransitionPagerTitleView.setNormalColor(getResources().getColor(R.color.grey));
            colorTransitionPagerTitleView.setSelectedColor(getResources().getColor(R.color.blue_press));
            colorTransitionPagerTitleView.setTextSize(15);
            colorTransitionPagerTitleView.setText(mArticleCategoryList.get(index).getCategoryTitle());
            colorTransitionPagerTitleView.setOnClickListener(view -> mViewPagerArticle.setCurrentItem(index));

            return colorTransitionPagerTitleView;
        }

        @Override
        public IPagerIndicator getIndicator(Context context) {
            LinePagerIndicator indicator = new LinePagerIndicator(context);
            indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
            indicator.setLineHeight(DensityUtils.dp2px(context, 1.5f));
            indicator.setLineWidth(UIUtil.dip2px(context, 16));
            indicator.setYOffset(DensityUtils.dp2px(context, 3));
            indicator.setColors(getResources().getColor(R.color.blue_press));
            return indicator;
        }
    };



    /**
     * 文章分类ViewPager的adapter。
     */
    class ArticleCategoryAdapter extends FragmentPagerAdapter {

        private List<ArticleCategory> mCategoryList;

        ArticleCategoryAdapter(FragmentManager fm, List<ArticleCategory> data) {
            super(fm);

            mCategoryList = data;
        }

        @Override
        public Fragment getItem(int i) {
            ArticleCategory category = mCategoryList.get(i);

            return ArticleListFragment.newInstance(category.getId(), "search", mSearchWord,0);
        }

        @Override
        public int getCount() {
            return mCategoryList.size();
        }
    }
}
